list gMenu=[];
list statesMenu=[0,0,0,0];
list gAvatarAllow;
key ToucherID;
integer gMenuLink =1337;
integer gDemon =1338;
integer gPlugin =1339;
integer gVic =1329;
integer channel_dialog;
integer listen_id;
string msg;
float gdistance;
list distance =["2","5","10","15","20","30","40"];
integer attachment = TRUE;
integer relaychannel = -1812221819;
string cmdname;
key victim;
integer crossing=0;
displayMenu()
{
	gMenu=[];
	if(llList2Integer(statesMenu,0)==1)gMenu+="[x]Range";
	else gMenu+="[ ]Range";
	gMenu+="Back";
	gMenu+=distance;
	msg = "Pick one";
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	llDialog(ToucherID, msg,gMenu, channel_dialog);
	
}

llRlvSay(string command)
{
	if(attachment==TRUE)
	{
		llOwnerSay("@"+command);
	}
	else
	{
		llSay(relaychannel,cmdname + "," + (string)victim  + "," + "@"+command);
	}
}

RequestPerms()
{
    if (llGetAttached())
    {
        llRequestPermissions(llGetOwner(), PERMISSION_TRACK_CAMERA);
    }
}

makeRandom()
{
	
	integer _rand;
	_rand = (integer)llFrand(99);
	llRlvSay("setenv_scenegamma:0."+(string)_rand+"=force");
	_rand = (integer)llFrand(99);
	llRlvSay("setenv_ambienti:0."+(string)_rand+"=force");
	_rand = (integer)llFrand(99);
	llRlvSay("setenv_ambientr:0."+(string)_rand+"=force");
	_rand = (integer)llFrand(99);
	llRlvSay("setenv_ambientg:0."+(string)_rand+"=force");
	_rand = (integer)llFrand(99);
	llRlvSay("setenv_ambientb:0."+(string)_rand+"=force");
	
}

makeDefault()
{

	llRlvSay("setenv_scenegamma:1.0=force");
	llRlvSay("setenv_ambientr:0.34=force");
	llRlvSay("setenv_ambientg:0.35=force");
	llRlvSay("setenv_ambientb:0.35=force");
	llRlvSay("setenv_ambienti:0.35=force");
}

llLRP(integer element_new, integer placeinlist)
{
	statesMenu = llListReplaceList(statesMenu, [element_new], placeinlist, placeinlist);
}

default {
    state_entry() 
    {
    	RequestPerms();
    }
     listen(integer channel, string name, key id, string message)
    {
    	if(channel==channel_dialog)
    	{
			llListenRemove(listen_id);
			if(message=="[x]Range")
			{
				llLRP(0,0);
				llSetTimerEvent(1.0);
				displayMenu();
			}
			else if(message=="[ ]Range")
			{
				llLRP(1,0);
				llSetTimerEvent(1.0);
				displayMenu();
			}
			else if(message=="Back")
			{
				llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_MISC", ToucherID);
			}
			else if(~llListFindList(distance, (list)message))
	  		{
	  			gdistance=(float)message;
	  			displayMenu();
	  		}
    	}
    }
    
    link_message(integer sender, integer num, string str, key id)
  	{
  		
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
  				ToucherID=id;
  				displayMenu();
  			}
  		}
  		if(num ==gDemon)
  		{
  			if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);
  			}
  		}
  	}
  	
  	on_rez(integer num)
    {
        RequestPerms();        
    }
  	

  	
  	timer()
  	{
  		if(llList2Integer(statesMenu,0)==1)
    	{
    		if(llGetPermissions() & PERMISSION_TRACK_CAMERA)
			{
				
				//disance
				vector campos = llGetCameraPos();
				vector position = llGetPos();
				float between = llVecDist(campos, position);
				if(between > gdistance)
				{
					llSetTimerEvent(1.0);
					llRlvSay("setenv=n");
					crossing=1;
					makeRandom();
				}
				else
				{
					if(crossing==1)
					{
						llSetTimerEvent(1.0);
						llRlvSay("setenv=y");
						makeDefault();
						crossing=0;
					}
				}
			}
    	}
    	else
    	{
    		if(crossing==1)
			{
				llSetTimerEvent(0.0);
				llRlvSay("setenv=y");
				makeDefault();
				crossing=0;
				
			}
    	}
  	}
}
